home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0012_PERMUTA3.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  3KB  |  95 lines

  1. {
  2. > I want to create all permutations.
  3.  
  4.  Okay. I should have first asked if you Really mean permutaions.
  5.  Permutations mean possible orders. I seem to recall your orginal message
  6.  had to do With card hands. They usually involve combinations, not
  7.  permutations. For example, all possible poker hands are the COMBinATIONS
  8.  of 52 cards taken 5 at a time. Bridge hands are the combinations of 52
  9.  cards taken 13 at a time. if you master the following Program, you should
  10.  be able to figure out how to create all combinations instead of
  11.  permutations.
  12.  
  13.  However, if you mean permutations, here is an example Program to produce
  14.  permutations. (You will have to alter it to your initial conditions.) It
  15.  involves a recursive process (a process which Uses itself). Recursive
  16.  processes are a little dangerous. It is easy to step on your own
  17.  privates writing them. They also can use a lot of stack memory. You
  18.  ought to be able to take the same general methods to produce
  19.  combinations instead of permutations if need be.
  20.  
  21.  I suggest you Compile and run the Program and see all the permutations
  22.  appear on the screen beFore reading further. (BTW, counts permutations
  23.  as well as producing them and prints out the count at the end.)
  24.  
  25.  The Procedure Permut below rotates all possible items into the first
  26.  Array position. For each rotation it matches the item With all possible
  27.  permutations of the remaining positions. Permut does this by calling
  28.  Permut For the Array of remaining positions, which is now one item
  29.  smaller. When the remaining Array is down to one position, only one
  30.  permutaion is possible, so the current Array is written out as one of
  31.  the results.
  32.  
  33.  Once you get such a Program working, it is theoretically possible to
  34.  convert any recursive Program to a non-recursive one. This often runs
  35.  faster. Sometimes the conversion is not easy, however.
  36.  
  37.  One final caution. The following Program Writes to the screen. You will
  38.  see that as the number of items increases, the amount of output
  39.  increases tremendously. if you were to alter the Program to Write
  40.  results to a File and to allow more than 9 items, you could easily
  41.  create a File as big as your hard drive.
  42. }
  43.  
  44. Program Permutes;
  45.  
  46. Uses
  47.   Crt;
  48.  
  49. Type
  50.   TArry = Array[1..9] of Byte;
  51.  
  52. Var
  53.   Arry : TArry;
  54.   Size,X : Word;
  55.   NumbofPermutaions : LongInt;
  56.  
  57. Procedure Permut(Arry : TArry; Position,Size : Word);
  58. Var
  59.   I,J : Word;
  60.   Swap: Byte;
  61. begin
  62.   if Position = Size then
  63. {  begin
  64.     For I := 1 to Size do
  65.       Write(Arry[I]:1);
  66. }    inc(NumbofPermutaions)
  67. {    Writeln
  68.   end
  69. }  else
  70.   begin
  71.     For J := Position to Size do
  72.     begin
  73.       Swap := Arry[J];
  74.       Arry[J] := Arry[Position];
  75.       Arry[Position] := Swap;
  76.       Permut(Arry,Position+1,Size)
  77.     end
  78.   end
  79. end;
  80.  
  81. begin
  82.   ClrScr;
  83.   Write('How many elements (1 to 9)? ');
  84.   readln(Size);
  85.    ClrScr;
  86.   For X := 1 to Size do
  87.     Arry[X] := X; {put item values in Array}
  88.   NumbofPermutaions := 0;
  89.   Permut(Arry,1,Size);
  90.   Writeln;
  91.   Writeln('Number of permutations = ',NumbofPermutaions);
  92.   Writeln('Press <Enter> to Exit.');
  93.   readln
  94. end.
  95.